﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MCorder.motor;
using System.Runtime.InteropServices;


namespace MCorder.programUI
{
    public partial class Form1 : Form
    {
        string[] args;

        public Form1(string[] argss)
        {
            this.args = argss;
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }
        private Motor MotorControl = new Motor();
        private void Form1_Load(object sender, EventArgs e)
        {
            MotorControl.Init();


            //timer1.Enabled = true;

        }       


        Int32 A, LPX, LPY, speed;
       
      
        private void button2_Click(object sender, EventArgs e)
        {
            MotorControl.InstStopRoll();
        }

        PCI1010.PCI1010_PARA_DataList DL;
        PCI1010.PCI1010_PARA_LCData LC;
        PCI1010.PCI1010_PARA_RR0 RR0;

        private void button4_Click(object sender, EventArgs e)
        {
            hDevice = PCI1010.PCI1010_CreateDevice(0);
            if (hDevice == (IntPtr)(-1))
            {
                MessageBox.Show("创建设备失败！");
                return;
            }

            LC.AxisNum = 2;					// 轴号(PCI1010_XAXIS:X轴; PCI1010_YAXIS:Y轴)
            LC.LV_DV = 1;
            LC.Mode = 0;					// 模式0：CW/CCW方式，1：CP/DIR方式 
            LC.Line_Curve = 0;				// 直线曲线(0:直线加/减速; 1:S曲线加/减速)
            DL.Multiple = 1;								// 倍数 (1~500) 
            DL.Acceleration = 2500;						// 加速度(125~1000,000)(直线加减速驱动中加速度一直不变）
            DL.AccelerationK = 2000;					// 加速度变化率(仅S曲线驱动时有效)
            DL.StartSpeed = 100;						// 初始速度(1~8000)
            DL.DriveSpeed = 8000;						// 驱动速度	(1~8000)	
            LC.nPulseNum = 150000;						// 定量输出脉冲数(0~268435455)
            LC.Direction = 1;			// 转动方向 PCI1010_PDirection: 正转  PCI1010_MDirection:反转		

            PCI1010.PCI1010_SetLP(hDevice, 1, 0);   // 逻辑计数器清零
            PCI1010.PCI1010_SetLP(hDevice, 2, 0);   // 实位计数器清零
            PCI1010.PCI1010_InitLVDV(				//	初始化连续,定长脉冲驱动
                                    hDevice,
                                    ref DL,
                                    ref LC);
            PCI1010.PCI1010_StartLVDV(				// 启动定长脉冲驱动
                                    hDevice,		// 设备句柄
                                    LC.AxisNum);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            PCI1010.PCI1010_DecStop(hDevice, 3); // 减速停止

        }

        private void button5_Click(object sender, EventArgs e)
        {
            MotorControl.StartRoll(2, 4000, 1, 5);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MotorControl.StartRoll(2, 4000,0, 5);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            double xx = Convert.ToDouble(textBox2.Text);
            MotorControl.setclutch(5, xx);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            double xx = Convert.ToDouble(textBox2.Text);
            MotorControl.setclutch(xx, 5);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;

           
            //roght
            {
                MotorControl.DIR = 1;
                MotorControl.setclutch(3.2, 4.5);
            }

            double xx = Convert.ToDouble(textBox4.Text);

            MotorControl.speed_Aim = xx;

            MotorControl.start();

        }

        private void button9_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            MotorControl.stop();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            //left
            {
                MotorControl.DIR = 0;
                MotorControl.setclutch(4.5, 3.2);
            }

            double xx = Convert.ToDouble(textBox4.Text);

            MotorControl.speed_Aim = xx;

            MotorControl.start();
        }

        IntPtr hDevice;
       // IntPtr hDevice8932;

        UInt32 CNTVal = 0;

        private void timer1_Tick(object sender, EventArgs e)
        {

            textBox1.Text = MotorControl.GetDistance().ToString();
            textBox3.Text = MotorControl.GetVelocity().ToString();
        }
    }
}
